/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.openjawtech.xscraper.scraper.runtime.threads;

import com.openjawtech.xscraper.schema.FlightsType;
import com.openjawtech.xscraper.scraper.runtime.xml.ScrapeXDB;
import com.openjawtech.xscraper.schema.scrape_config.ScrapeGroupsType;
import com.openjawtech.xscraper.flights.PersistService;
import com.openjawtech.xscraper.flights.ServiceFactory;
import com.openjawtech.xscraper.schema.FlightsDocument;
import com.openjawtech.xscraper.scraper.runtime.HtmlScraper;
import com.openjawtech.xscraper.scraper.filter.HtmlFileFilter;
import com.openjawtech.xscraper.scraper.filter.XmlFileFilter;
import com.openjawtech.xscraper.scraper.StartupConfiguration;
import java.io.IOException;
import java.util.List;
import java.io.File;
import java.util.Calendar;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;

/**
 *
 * @Created on 26-Apr-2009, 07:32:21
 * @author Wenbo.Sun
 */
public class ScrapeThread extends Thread {

    private File dir;
    public static final String SCRAPEXML = "scrape.xml";
    private ActionType actionType;
    private static final Log log = LogFactory.getLog(ScrapeThread.class);
    private static final PersistService persistService = ServiceFactory.createPersistService();

    public ScrapeThread(File dir, ActionType actionType) {
        this.dir = dir;
        this.actionType = actionType;
    }

    @Override
    public void run() {
        ScrapeXDB sxdb = new ScrapeXDB();
        File scrapeXmlFile = new File(dir.getPath() + "/" + SCRAPEXML);
        long lastModified = 0;
        ScrapeGroupsType sgt = null;
        long scrapeXmlLast = 0;
        while (true) {
            log.debug(dir.getPath());
            try {
                sleep(5000);
            } catch (InterruptedException ex) {
                log.error(dir.getPath(), ex);
            }
            if (lastModified == dir.lastModified()) {
                continue;
            }
            switch (actionType) {
                case HTMLScrape:
                    if (scrapeXmlFile.exists()) {
                        if (scrapeXmlLast != scrapeXmlFile.lastModified()) {
                            sgt = sxdb.unMarshall(scrapeXmlFile);
                        }
                        scrape(sgt);
                        scrapeXmlLast = scrapeXmlFile.lastModified();
                        break;
                    }
                    break;
                case XMLScrape:
                    persistXml();
                    break;
                case XMLDelete:
                    deleteXml();
                    break;

            }
            lastModified = dir.lastModified();
        }
    }

    private void scrape(ScrapeGroupsType sgt) {
        for (File file : dir.listFiles(new HtmlFileFilter())){
            HtmlScraper scraper = new HtmlScraper (sgt, file);
            List<FlightsType> list = scraper.scrape();
            persist(list);
            boolean bool = file.delete();
            if (bool == false) {
                file.deleteOnExit();
            }
        }
    }

    private void persist(List<FlightsType> list) {
        for (FlightsType flightsType : list) {
            File file = new File(StartupConfiguration.SAVEDFOLDER + "/" + Calendar.getInstance().getTimeInMillis() + ".xml");
            try {
                flightsType.save(file);
            } catch (IOException ex) {
                log.error("save to xml failed!", ex);
            }
            persistService.persist(flightsType);
        }
    }

    private void persistXml() {
        for (File file: dir.listFiles(new XmlFileFilter())){
            persistService.persist(file);
            file.delete();
        }
    }

    private void deleteXml() {
        for (File file: dir.listFiles(new XmlFileFilter())){
            try {
                FlightsDocument doc = FlightsDocument.Factory.parse(file);
                FlightsType flights = doc.getFlights();
                persistService.removeFlights(flights);
                file.delete();
            } catch (XmlException ex) {
                log.error("delete failed", ex);
            } catch (IOException ex) {
                log.error("delete failed", ex);
            }
        }
    }
}
